clear all
set mem 9999999
set more off
set matsize 10000




cd "$dir\Raw_Data"

use base_with_religion.dta



capture drop _merge
merge m:1 school1 using "Schools12.dta"
drop _merge
merge m:1 school1 using languagetest.dta
replace language="Dual" if language=="dual"
replace language="" if language=="N/A"
tab language, gen(lang)
drop _merge
merge m:1 school1 using tuitiontest.dta
drop _merge

cd "$dir\Final_Data"
  
drop  islam_school christ_school stmusl  stcath christianchrist3 christianislam3   islamicchrist3 islamicislam3
*drop _merge
merge m:1 ID using Final_Sample.dta, keepusing(islam_school christ_school stmusl  stcath christianchrist3 christianislam3   islamicchrist3 islamicislam3)
*drop if _merge==2
  
  

  
*gen course indicator
  	gen course=Subject + CourseNum
		encode course, gen(course1)

	gen course_term=course1 +Term
egen group_co_term =group(course_term)

	gen course_year=course1 +year
egen group_co_year =group(course_year)

encode(Dept), gen(dpt)
encode(Rank), gen(Rank1)

global treat islam_school christ_school stmusl  stcath ///
 christianchrist3 christianislam3  ///
 islamicchrist3 islamicislam3
 
cd "$dir\Raw_Data"


 drop _merge
 merge m:1 Term Subject CourseNum instructor_sex using teach_name.dta, keepusing(name1)
keep if _merge==3

gen CourseName= Subject+CourseNum
bys CourseName year: egen mGrade= mean(grade)
bys CourseName year: egen sdGrade= sd(grade)
gen stGrade= (grade-mGrade)/sdGrade
egen teach_id=group(name1)

egen stsatmat=std(satmat)
egen stsatverb=std(satverb)
gen satmat2=stsatmat^2
gen satmat3=stsatmat^3
gen satverb2=stsatverb^2
gen satverb3=stsatverb^3
bys Term Subject CourseNum instructor_sex: egen class_satmat=mean(stsatmat)
gen class_satmat2=class_satmat^2
gen class_satmat3=class_satmat^3
bys Term Subject CourseNum instructor_sex: egen class_satverb=mean(stsatverb)
gen class_satverb2=class_satverb^2
gen class_satverb3=class_satverb^3
bys dpt Term: egen dept_satmat=mean(stsatmat)
bys dpt Term: egen dept_satverb=mean(stsatverb)
gen dept_satmat2=dept_satmat^2
gen dept_satmat3=dept_satmat^3
gen dept_satverb2=dept_satverb^2
gen dept_satverb3=dept_satverb^3


areg stGrade stsatmat stsatverb satmat2 satmat3 satverb2 satverb3  sex* legacy advsex* dept_satmat dept_satverb dept_satmat2 dept_satmat3 dept_satverb2 dept_satverb3 class_satmat class_satverb class_satmat2 class_satmat3 class_satverb3 class_satverb2 i.dpt##i.year if bla>10 & bla<=50, abs(teach_id)

 predict score_res, dr
bys teach_id: egen va=mean(score_res)


cd "$dir\Tables"	

global treat islam_school christ_school stmusl  stcath ///
 christianchrist3 christianislam3  ///
 islamicchrist3 islamicislam3
 
reghdfe va $treat satmat satverb sex* legacy advsex*   if year>=2012 & year<=2017 & FR_nonFR_status =="never enrolled as FR" & satmat!=. & satverb!=. & MAJR_CODE!="MJRL"  & bla>10 & bla<=30 , abs(i.dpt i.dpt##c.year year Rank1)   cluster(advisorIDyear)
eststo zab
** PEERS EFFECTS for STUDEnTS FROM SECULAR Schools*** (Secular match)
lincomest stcath //All Secular schools peers relative to all Christian schools peers
eststo row11
estimates restore zab
lincomest stmusl  //All Secular schools peers relative to all Islamic schools peers
eststo  row21
estimates restore zab
** PEERS EFFECTS for STUDEnTS FROM CHRISTIAN Schools*** (Christian match)
lincomest -stcath-christianchrist3  //All Christian schools peers relative to all Secular schools peers
eststo row31
estimates restore zab
lincomest  -stcath-christianchrist3+stmusl+christianislam3 //All Christian schools peers relative to all Islamic schools peers
eststo row41
estimates restore zab
** PEERS EFFECTS for STUDEnTS FROM ISLAMIC Schools*** (Islam match)
lincomest  -stmusl-islamicislam3 //All Islamic schools peers relative to all Secular schools peers7
eststo row51
estimates restore zab
lincomest  -stmusl-islamicislam3+stcath+islamicchrist3 //All Muslim school peers relative to all Christian schools peers
eststo row61
estimates restore zab









reghdfe va $treat satmat satverb sex* legacy if year>=2012 & year<=2017 & FR_nonFR_status =="never enrolled as FR" & satmat!=. & satverb!=. & MAJR_CODE!="MJRL"  & bla>10 & bla<=30 , abs(i.dpt##i.year fake_advisor_id)  cluster(advisorIDyear)
eststo zab
** PEERS EFFECTS for STUDEnTS FROM SECULAR Schools*** (Secular match)
lincomest stcath //All Secular schools peers relative to all Christian schools peers
eststo row12
estimates restore zab
lincomest stmusl  //All Secular schools peers relative to all Islamic schools peers
eststo  row22
estimates restore zab
** PEERS EFFECTS for STUDEnTS FROM CHRISTIAN Schools*** (Christian match)
lincomest -stcath-christianchrist3  //All Christian schools peers relative to all Secular schools peers
eststo row32
estimates restore zab
lincomest  -stcath-christianchrist3+stmusl+christianislam3 //All Christian schools peers relative to all Islamic schools peers
eststo row42
estimates restore zab
** PEERS EFFECTS for STUDEnTS FROM ISLAMIC Schools*** (Islam match)
lincomest  -stmusl-islamicislam3 //All Islamic schools peers relative to all Secular schools peers7
eststo row52
estimates restore zab
lincomest  -stmusl-islamicislam3+stcath+islamicchrist3 //All Muslim school peers relative to all Christian schools peers
eststo row62
estimates restore zab




esttab row1* using TableB5.tex, label  nocon  b(%9.4f) se(%9.4f) starlevels(* 0.1 ** 0.05 *** 0.01) replace
foreach i in  2 3 4 5 6{
esttab row`i'* using TableB5.tex, label  nocon  b(%9.4f) se(%9.4f) starlevels(* 0.1 ** 0.05 *** 0.01) append
}

